package cn.schoolwow.workflow.module.task.action.flow.complete.step;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import cn.schoolwow.workflow.domain.deploy.WorkFlowDefinitionOption;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.entity.WorkFlowDefinition;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowTask;

public class QueryCompleteTaskInstanceFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkRequest(flowContext);
        setWorkFlowTask(flowContext);
        setWorkFlowInstance(flowContext);
        setWorkFlowDefinition(flowContext);
    }

    @Override
    public String name() {
        return "查询完成实例所需相关实例";
    }

    private void checkRequest(FlowContext flowContext){
        CompleteTaskRequest completeTaskRequest = flowContext.checkInstanceData(CompleteTaskRequest.class);

        CheckInstance.newCheck()
                .notNullCheck(completeTaskRequest.taskId, "任务id")
                .notNullCheck(completeTaskRequest.taskType, "完成任务类型");
    }

    private void setWorkFlowTask(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        CompleteTaskRequest completeTaskRequest = flowContext.checkInstanceData(CompleteTaskRequest.class);

        dao.addRecordString("根据任务id查询工作流任务记录");
        WorkFlowTask workFlowTask = dao.query(WorkFlowTask.class)
                .addQuery("id", completeTaskRequest.taskId)
                .execute()
                .getOne();
        if(null==workFlowTask){
            throw new IllegalArgumentException("工作流任务不存在!id:"+completeTaskRequest.taskId);
        }
        flowContext.putTemporaryInstanceData(workFlowTask);
    }

    private void setWorkFlowInstance(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        WorkFlowTask workFlowTask = flowContext.checkInstanceData(WorkFlowTask.class);
        CompleteTaskRequest completeTaskRequest = flowContext.checkInstanceData(CompleteTaskRequest.class);

        dao.addRecordString("根据实例id查询工作流实例记录");
        WorkFlowInstance workFlowInstance = dao.query(WorkFlowInstance.class)
                .addQuery("id", workFlowTask.getInstanceId())
                .execute()
                .getOne();
        if(null==workFlowInstance){
            throw new IllegalArgumentException("工作流实例不存在!id:"+workFlowTask.getInstanceId());
        }
        if(WorkFlowInstanceStatus.Finished.status==workFlowInstance.getStatus()){
            throw new IllegalArgumentException("任务所属流程实例状态为已完成!任务id:"+completeTaskRequest.taskId+",工作流实例名称:"+workFlowInstance.getName());
        }
        workFlowInstance.getContextData().putAll(completeTaskRequest.contextData);
        flowContext.putTemporaryInstanceData(workFlowInstance);
    }

    private void setWorkFlowDefinition(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);

        dao.addRecordString("根据实例id查询工作流实例记录");
        WorkFlowDefinition workFlowDefinition = dao.query(WorkFlowDefinition.class)
                .addQuery("id", workFlowInstance.getDefinitionId())
                .execute()
                .getOne();
        if(null==workFlowDefinition){
            throw new IllegalArgumentException("工作流定义不存在!id:"+workFlowInstance.getDefinitionId());
        }
        flowContext.putTemporaryInstanceData(workFlowDefinition);

        if(null!=workFlowDefinition.getOption()){
            WorkFlowDefinitionOption workFlowDefinitionOption = workFlowDefinition.getOption().toJavaObject(WorkFlowDefinitionOption.class);
            flowContext.putTemporaryInstanceData(workFlowDefinitionOption);
        }
    }
}
